package com.eeepay.eeepay_v2.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.util.Constant;


/**
 * Created by zw on 2016/4/12 0012.
 * 地区选择控件
 */
public class AreaSelectView extends PopupWindow implements View.OnClickListener {

    private View view;
    private Context mContext;
    private CityPicker cityPicker;
    private TextView cancel, confirm;//取消、 确定
    private View viewLayout;

    private OnAreaSelectedListener listener;

    public AreaSelectView(Context context){
        this.mContext = context;
        init();
        setWidget();
    }

    private void init() {
        view = View.inflate(mContext, R.layout.custom_area_select_view, null);
        this.setContentView(view);
        this.setFocusable(true);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //菜单背景色
        ColorDrawable dw = new ColorDrawable(Constant.GRAY_LUCENCY_COLOR);
        this.setBackgroundDrawable(dw);

        cityPicker = (CityPicker) view.findViewById(R.id.citypicker);
        cancel = (TextView) view.findViewById(R.id.tv_cancel);
        confirm = (TextView) view.findViewById(R.id.tv_confirm);
        viewLayout =  view.findViewById(R.id.layout);
    }

    private void setWidget() {
        cancel.setOnClickListener(this);
        confirm.setOnClickListener(this);
        viewLayout.setOnClickListener(this);
    }

    public void setOnAreaSelectedListener(OnAreaSelectedListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_confirm:
                if(listener != null){
                    listener.onSelected(cityPicker.getCity_string());
                }
                break;
            case R.id.layout:
                this.dismiss();
        }
    }

    public String getCitySelected(){
        return cityPicker.getCitySelected();
    }

    public String getProvinceSelected(){return cityPicker.getProvinceSelected();}

    public String getCounySelected(){
        return cityPicker.getCounySelected();
    }

    @Override
    public void dismiss() {
        if (this.isShowing()) {
            super.dismiss();
//            WindowManager.LayoutParams params = ((Activity) mContext).getWindow().getAttributes();
//            params.alpha = 1f;
//            ((Activity) mContext).getWindow().setAttributes(params);
        }
    }

    /**
     * 设置页面的透明度
     * @param bgAlpha 1表示不透明
     */
    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            //不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        activity.getWindow().setAttributes(lp);
    }

    public interface OnAreaSelectedListener{
        void onSelected(String area);
    }
}
